package gold.enterprise;

import java.util.*;

public class Microsoft_Other {

    public static int findLongestSubSeq(int[] arr) {
        if (null == arr || arr.length <= 0) return 0;
        int ans = 0, left = 0, right = 0, curWindowNum = arr[0], curWindowUnMatchCnt = 0;
        Map<Integer, Integer> prev = new HashMap<>();// 此处记录当前窗口数字出现的最早位置
        while (right < arr.length) {
            if (arr[right] != curWindowNum) {
                curWindowUnMatchCnt++;
                if (curWindowUnMatchCnt <= 1 && prev.containsKey(curWindowNum) && prev.get(curWindowNum) < left) {
                    // 仅此种情况可以统计长度
                    ans = Math.max(ans, right - left + 1);
                } else {
                    // 需要扩张窗口
                    prev.put(arr[left], left);
                    left = right;
                    curWindowNum = arr[right];
                    curWindowUnMatchCnt = 0;
                }
            } else {
                ans = Math.max(ans, right - left + 1);
            }

            right++;
        }

        return Math.max(ans, right - left + 1);
    }

    public static void main(String[] args) {
        int[] arr = new int[]{2, 2, 3, 1, 4, 5, 1, 1, 0, 1, 7};
        int ans = findLongestSubSeq(arr);
        System.out.println(ans);
    }

}
